package com.mycase.im.boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycase.im.boot.entity.database.ImChatGroup;
import com.mycase.im.boot.exception.CustomException;
import com.mycase.im.boot.mapper.ChatGroupMapper;
import com.mycase.im.boot.service.ChatGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author ZhuHJay
 * @Date 2021/9/2 17:47
 */
@Service
@Transactional(rollbackFor = CustomException.class)
public class ChatGroupServiceImpl extends ServiceImpl<ChatGroupMapper, ImChatGroup> implements ChatGroupService {

    @Override
    public ImChatGroup createGroup(String createId, String groupName) {
        // 创建JavaBean
        ImChatGroup imChatGroup = new ImChatGroup();
        imChatGroup.setCreateUser(createId);
        imChatGroup.setCreateTime(new Date());
        imChatGroup.setName(groupName);
        // 增加数据库
        boolean save = save(imChatGroup);
        if( !save ){
            throw new CustomException("创建失败, 请重试");
        }
        return imChatGroup;
    }

    @Override
    public Boolean isExist(String id) {
        return getById(id) != null;
    }
}
